﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RooHui.Tool;
using RooHui.Infrastructure.Entities.Entity;
using RooHui.Infrastructure.Entities.EFContext;
using RooHui.Infrastructure.Entities.Enum;
using RooHui.Infrastructure.Entities.Repository;

namespace RooHui.OA.Biz
{
    public class ProjectPublishBiz
    {
        public static List<Project> GetProjectListForPublish(int page, int pageSize, int guaranteeCompanyID, string key, string userName, ref int count)
        {
            try
            {
                using (Context ctx = new Context())
                {
                    var expression = from c in ctx.Project
                                     join d in ctx.Product
                                     on c.ProductID equals d.ID
                                     join e in ctx.GuaranteeCompany
                                     on c.GuaranteeCompanyID equals e.ID
                                     join f in ctx.Enterprise
                                     on c.EnterpriseID equals f.ID
                                     join k in ctx.ProjectPolicy
                                     on c.ID equals k.ProjectId
                                     join j in ctx.Policy.Where(m => m.IsAttach == false)
                                     on k.PolicyId equals j.ID
                                     join h in ctx.Member
                                     on c.BorrowerName equals h.UserName into ht
                                     from hi in ht.DefaultIfEmpty()
                                     join l in ctx.Account
                                     on hi.UserName equals l.UserName into lt
                                     from li in lt.DefaultIfEmpty()
                                     where (e.ID == guaranteeCompanyID || guaranteeCompanyID == -1)
                                     && (c.ProjectCode.Contains(key) || c.ProjectTitle.Contains(key)
                                     || d.ProductName.Contains(key) || f.EnterpriseCode.Contains(key)
                                     || f.EnterpriseName.Contains(key))
                                     && c.Status == (int)ProjectStatus.Pass
                                     orderby c.CreateTime
                                     select new { c, d, e, f, j, hi, li };
                    count = expression.Count();
                    var data = expression
                        .Skip((page - 1) * pageSize)
                        .Take(pageSize)
                        .ToList().Select(p => new Project().Set(p.c).LoadProduct(p.d).LoadGuaranteeCompany(p.e).LoadEnterprise(p.f).LoadPolicy(p.j).LoadBorrowerUser(p.hi, p.li)).ToList();
                    return data;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        public static int AddMemberForPublish(int projectID, string userName, string realName,
            string idCardNo, string handPhone, string address, string createUser)
        {
            int flag = 0;
            try
            {
                Gender gender = Gender.Unknow;
                if (SupportBiz.CheckIDCardNo(idCardNo, ref gender))
                {
                    if (MemberBiz.AgentExsit(userName))
                    {
                        return -1;//用户名已存在
                    }
                    var member = new Member();
                    member.Address = address;
                    member.CreateTime = DateTime.Now;
                    member.CreateType = CreateType.Mortgagor;
                    member.CreateUser = createUser;
                    member.Email = userName;
                    member.EmailCheck = true;
                    member.Gender = gender;
                    member.HandPhone = handPhone;
                    member.HandPhoneCheck = true;
                    member.IDCardNo = idCardNo;
                    member.IDCardCheck = true;
                    member.IsActive = true;
                    member.MemberType = MemberType.Borrower;
                    member.Password = AesAlgorithm.Encrypt(idCardNo.Substring(12, 6));
                    member.RealName = realName;
                    member.Status = ItemStatus.Enable;
                    member.UserName = userName;
                    member = member.PostAdd();

                    //更新项目信息,项目在第三方开户成功后变更为发布状态
                    var project = new Project().Find(projectID.ToString());
                    project.BorrowerName = member.UserName;
                    project.PostModify();
                }
                else
                {
                    return -2;//身份证格式错误哦
                }
                return 1;
            }
            catch (Exception)
            {
                flag = 0;
            }
            return flag;
        }

        public static int ProjectPublish(int projectID, DateTime realeseTime, DateTime beginDate, DateTime borrowInterestDate)
        {
            int flag = 0;
            try
            {
                var project = new Project().Find(projectID.ToString());
                project.ReleaseTime = realeseTime;
                project.BeginDate = beginDate;
                project.BorrowInterestDate = borrowInterestDate;
                project.Status = ProjectStatus.Publish;
                project.PostModify();
                if (project != null)
                {
                    return 1;
                }

            }
            catch (Exception)
            {
                flag = 0;
            }
            return flag;
        }

    }
}
